package com.sywb.chuangyebao.contract;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.bean.CategoryBean;
import com.sywb.chuangyebao.bean.CategorySection;
import com.sywb.chuangyebao.utils.HttpCallback;
import com.sywb.chuangyebao.utils.HttpUtils;
import com.sywb.chuangyebao.view.SearchJoinActivity;
import com.sywb.chuangyebao.widget.GridSpacingItemDecoration;

import org.bining.footstone.adapter.BaseRecyclerAdapter;
import org.bining.footstone.adapter.BaseRecyclerSectionAdapter;
import org.bining.footstone.adapter.ViewHolderHelper;
import org.bining.footstone.adapter.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by pxb on 2018/6/11.
 * 全部分类
 */

public interface AllCategoryContract {

    interface AllCategoryView extends BaseRecyclerContract.View {
        RecyclerView getTypesRv();

        LinearLayout getAbl();

        String getClickID();

    }

    class AllCategoryPresenter extends BaseRecyclerContract.Presenter<AllCategoryView> {
        private RecyclerView typesRv;
        private CategoryTypesAdapter categoryTypesAdapter = null;//一级
        private int[] selectImage = new int[]{R.drawable.classify_food, R.drawable.classify_clothing, R.drawable.classify_life, R.drawable.classify_drink, R.drawable.classify_furnishing,
                R.drawable.classify_cosme, R.drawable.classify_car, R.drawable.classify_decorate, R.drawable.classify_gift,
                R.drawable.classify_protection, R.drawable.classify_baby, R.drawable.classify_education, R.drawable.classify_education};
        private CategoryListAdapter categoryListAdapter = null;//二级
        private LinearLayout ablLl;
        private List<CategoryBean> allCategoryBeans = new ArrayList<>();
        private List<CategorySection> allSearchSetions;
        private int toPosition;

        @Override
        public void onStart() {
            super.onStart();
            categoryTypesAdapter = new CategoryTypesAdapter(mContext);
            categoryListAdapter = new CategoryListAdapter(mContext);
            typesRv = mView.getTypesRv();
            ablLl = mView.getAbl();
            initView();
            typesRv.setLayoutManager(new LinearLayoutManager(mContext));
        }

        private void initView() {
            categoryTypesAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(View view, int i) {
                    categoryTypesAdapter.setSelectionPosition(i);
                    for (int j = 0; j < allSearchSetions.size(); j++) {
                        if (allCategoryBeans.get(i).getName().equals(allSearchSetions.get(j).header)) {
                            //先滚动到指定位置
                            commonRecycler.smoothScrollToPosition(j);
                            //toPosition =j;
                        }
                    }
                }
            });
            GridSpacingItemDecoration gridSpacingItemDecoration = new GridSpacingItemDecoration(mActivity.getResources().getDimensionPixelSize(R.dimen.dp_15), mActivity.getResources().getDimensionPixelSize(R.dimen.dp_15), false);
            commonRecycler.addItemDecoration(gridSpacingItemDecoration);
            getAllCategory();
            commonRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);
                    /*if (newState== 0){
                        GridLayoutManager gridLayoutManager = (GridLayoutManager)commonRecycler.getLayoutManager();
                       int  firstVisibleItemPosition = gridLayoutManager.findFirstVisibleItemPosition();
                        int top = commonRecycler.getChildAt(toPosition - firstVisibleItemPosition).getTop();
                        commonRecycler.smoothScrollBy(0,top);
                    }*/
                }

                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
                    GridLayoutManager layoutManager = (GridLayoutManager) commonRecycler.getLayoutManager();
                    int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
                    int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
                    if (dy > 0) {
                        if (!categoryListAdapter.getItem(firstVisibleItemPosition).isHeader) {
                            if (categoryListAdapter.getItem(firstVisibleItemPosition + 1).isHeader) {
                                for (int i = 0; i < allCategoryBeans.size(); i++) {
                                    if (allCategoryBeans.get(i).getName().equals(categoryListAdapter.getItem(firstVisibleItemPosition + 1).header)) {
                                        typesRv.scrollToPosition(i);
                                    }
                                }
                            }
                        }
                    } else {

                    }
                }
            });
            /*if (allCategoryBeans != null) {
                for (int i = 0; i < allCategoryBeans.size(); i++) {
                    if (Integer.parseInt(mView.getClickID()) == allCategoryBeans.get(i).getId()) {
                        categoryTypesAdapter.setSelectionPosition(i);
                    }
                }
            }*/
        }

        @Override
        public int useRecyclerLayoutManager() {
            return 2;
        }

        @Override
        public int useGridLayoutManagerColumns() {
            return 2;
        }

        @Override
        public void onRecyclerItemClick(int postion) {
            super.onRecyclerItemClick(postion);
            if (!categoryListAdapter.getItem(postion).isHeader) {
                mView.advance(SearchJoinActivity.class, categoryListAdapter.getItem(postion).t
                        .getParent_id() + "", allCategoryBeans, categoryListAdapter.getItem(postion)
                        .t.getId() + "");
            }
        }

        @Override
        public boolean useRecyclerDivider() {
            return false;
        }

        private void getAllCategory() {
            HttpUtils.getAllCategoryList(new HttpCallback<List<CategoryBean>>() {
                @Override
                public void onStart() {
                    super.onStart();
                    ablLl.setVisibility(View.INVISIBLE);
                    mView.showProgress();
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    if (mView != null)
                        mView.hideProgress();
                }

                @Override
                public void onError(String strMsg) {
                    super.onError(strMsg);
                    showMessage(strMsg);
                }

                @Override
                public void onSuccess(List<CategoryBean> categoryBeanList) {
                    ablLl.setVisibility(View.VISIBLE);
                    if (categoryBeanList != null) {
                        allCategoryBeans = categoryBeanList;
                        categoryTypesAdapter.addDatas(categoryBeanList);
                        typesRv.setAdapter(categoryTypesAdapter);
                        getSearchSections();
                    }
                    if (allCategoryBeans != null) {
                        for (int i = 0; i < allCategoryBeans.size(); i++) {
                            if (Integer.parseInt(mView.getClickID()) == allCategoryBeans.get(i).getId()) {
                                categoryTypesAdapter.setSelectionPosition(i);
                                typesRv.scrollToPosition(i);
                            }
                        }
                    }
                }
            });
        }

        private void getSearchSections() {
            allSearchSetions = new ArrayList<>();
            for (int i = 0; i < allCategoryBeans.size(); i++) {
                allSearchSetions.add(new CategorySection(true, selectImage[i], allCategoryBeans.get(i).getName()));
                List<CategoryBean.ListBean> listBeans = allCategoryBeans.get(i).getList();
                for (int j = 0; j < listBeans.size(); j++) {
                    allSearchSetions.add(new CategorySection(j, listBeans.get(j)));
                }
            }
            categoryListAdapter.setDatas(allSearchSetions);
            setRecyclerAdapter(categoryListAdapter);
            GridLayoutManager layoutManager = (GridLayoutManager) commonRecycler.getLayoutManager();
            layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    return categoryListAdapter.getItem(position).isHeader ? 2 : 1;
                }
            });

        }

        class CategoryTypesAdapter extends BaseRecyclerAdapter<CategoryBean> {
            private int oldPosition = 0;

            public CategoryTypesAdapter(Context context) {
                super(context, R.layout.item_all_category);
            }

            @Override
            protected void setItemData(ViewHolderHelper viewHolderHelper, int position, CategoryBean resultBean) {
                viewHolderHelper.setText(R.id.item_category_tv, resultBean.getName());
                if (oldPosition == position) {
                    viewHolderHelper.setBackgroundColor(R.id.item_category_ll, ContextCompat.getColor(mContext, R.color.white));
                    viewHolderHelper.setVisibility(R.id.item_category_indicator_view, true);
                } else {
                    viewHolderHelper.setBackgroundColor(R.id.item_category_ll, ContextCompat.getColor(mContext, R.color.colorManuscript));
                    viewHolderHelper.setVisibility(R.id.item_category_indicator_view, false);
                }
            }

            public void setSelectionPosition(int position) {
                this.oldPosition = position;
                notifyDataSetChanged();
            }
        }

        class CategoryListAdapter extends BaseRecyclerSectionAdapter<CategorySection> {

            public CategoryListAdapter(Context context) {
                super(context, R.layout.layout_category_header, R.layout.item_category, null);
            }

            @Override
            protected void setHeadData(ViewHolderHelper viewHolderHelper, int i, CategorySection categorySection) {
                viewHolderHelper.setText(R.id.category_list_tv, categorySection.header);
                viewHolderHelper.setBackgroundRes(R.id.category_list_iv, categorySection.id);
            }

            @Override
            protected void setContentData(ViewHolderHelper viewHolderHelper, int i, CategorySection categorySection) {
                viewHolderHelper.setText(R.id.item_tv, categorySection.t.getName());
            }
        }
    }
}
